//
	// $Id: BaseTest2.java,v 1.8 2007/05/02 21:34:06 vivaldi Exp $
//
// BUI - a user interface library for the JME 3D engine
// Copyright (C) 2005, Michael Bayne, All Rights Reserved
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package eMdP.estats;

import com.jme.input.joystick.JoystickInput;
import com.jme.system.DisplaySystem;
import com.jmex.bui.BButton;
import com.jmex.bui.BCheckBox;
import com.jmex.bui.BComboBox;
import com.jmex.bui.BLabel;
import com.jmex.bui.BList;
import com.jmex.bui.BComboBox.Item;
import com.jmex.bui.event.ActionEvent;
import com.jmex.bui.event.ActionListener;
import com.jmex.bui.util.Point;

import eMdP.ElMensajeroDePekin;
import eMdP.gestors.GestorConstants;
import eMdP.gestors.GestorFitxers;
import eMdP.gestors.GestorPantalla;

/**
 * A base class for our various visual tests.
 */
public class MenuConfiguracio extends GbuiGameState {
	
	private BCheckBox checkBox;
	private BLabel text;
	private BButton boto;
	private BList llista;
	private BComboBox comboBox;
    
    public MenuConfiguracio(String name)
    {
    	super(name);
    	
        window.setSize((int)(display.getWidth()-10), (int)(display.getHeight()-10));
        
        int buttonWidth = window.getWidth()/4;
 		int buttonHeight = GestorConstants.ALT_BOTO;
 		int posicioY = window.getHeight()-buttonHeight;
 		int posicioX = window.getWidth()/8;
        
 		// Controls
 		text = new BLabel("Controls");
 		text.setPreferredSize(180, buttonHeight);
 		window.add(text, new Point(10, posicioY));
 		
 		posicioY = posicioY-GestorConstants.ALT_BOTO-10;
 		
 		for(int i=0;i<4;i++)
 		{
 			text = new BLabel("Jugador "+(i+1));
 	 		text.setPreferredSize(150, buttonHeight);
 	 		window.add(text, new Point(10+(160*i),posicioY));
 		}
 		
 		posicioY = posicioY-GestorConstants.ALT_BOTO-10;
 		creaLlistaControls(posicioY);
 		
 		posicioY = posicioY-GestorConstants.ALT_BOTO-10;
 		crearLlistaResolucio(posicioY);
 		
 		posicioY = posicioY-GestorConstants.ALT_BOTO*2-10;
 		crearCheckBoxAudio(posicioY);
 		
 		posicioY = posicioY-GestorConstants.ALT_BOTO-10;
 		crearCheckBoxPantallaCompleta(posicioY);
                
 		// Cancel button
        boto = new BButton("Cancel·lar");
        boto.setPreferredSize(buttonWidth, buttonHeight);
        boto.addListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent event) {
 				GestorPantalla.canvia(2,0);
 			}
         });
         window.add(boto, new Point(posicioX, 20));
         
         // Boto acceptar
         posicioX = window.getWidth()/8*5;
         boto = new BButton("Guardar");
         boto.setPreferredSize(buttonWidth, buttonHeight);
         boto.addListener(new ActionListener() {
  			@Override
  			public void actionPerformed(ActionEvent event) {
  				String[] controls = new String[4];
  				for(int i=0;i<4;i++)
  				{
  					try
  					{
  						controls[i] = ((BComboBox)window.getComponent(i+5)).getSelectedValue().toString();
  					}
  					catch (Exception e)
  					{
  						System.out.println("MenuConfiguracio::Problema el llegir el control "+(i+1));
  					}
  				}
  				ElMensajeroDePekin.setControls(controls);
  				try
  				{
  					String resolucioString=((BList)window.getComponent(10)).getSelectedValue().toString();
  					int[] resolucio = new int[2];
  					if(resolucioString.equals("800x600"))
  					{
  						resolucio[0]=800; resolucio[1]=600;
  					}
  					else
  					{
  						resolucio[0]=1024; resolucio[1]=768;
  					}
  					ElMensajeroDePekin.setResolucio(resolucio);
  				}
  				catch (Exception e)
  				{
  					System.out.println("MenuConfiguracio::Problema al llegir la resolucio");
  				}
  				try
  				{
  					ElMensajeroDePekin.setSo(((BCheckBox)window.getComponent(12)).isSelected());
  				}
  				catch (Exception e) {}
  				try
  				{
  					ElMensajeroDePekin.setPantallaCompleta(((BCheckBox)window.getComponent(14)).isSelected());
  				}
  				catch (Exception e) {}
  				
  				GestorFitxers.guardarConfiguracio();
  				ElMensajeroDePekin.reinicia();
  				GestorPantalla.canvia(2,0);
  			}
         });
         window.add(boto, new Point(posicioX, 20));
    }
    
    private void creaLlistaControls(int posicioY)
    {
    	for(int i=0;i<4;i++)
    	{
    		comboBox = new BComboBox();
    		comboBox.setPreferredSize(150,GestorConstants.ALT_BOTO);
    		Item item = new Item("teclat_1", "Teclat 1");
    		//item.setStyleClass("item");
    		comboBox.addItem(item);
    		item = new Item("teclat_2", "Teclat 2");
    		//item.setStyleClass("item");
    		comboBox.addItem(item);
    		
    		for(int j=0;j<JoystickInput.get().getJoystickCount();j++)
    		{
    			String joystick = JoystickInput.get().getJoystick(j).getName();
    			item = new Item(joystick, joystick);
    			//item.setStyleClass("item");
        		comboBox.addItem(item);
    		}
    			System.out.println();
        	window.add(comboBox, new Point(10+(i*160),posicioY));
    	}
    }
    
    private void crearLlistaResolucio(int posicioY)
    {
    	text = new BLabel("Resolucio");
    	text.setPreferredSize(180, GestorConstants.ALT_BOTO);
    	window.add(text, new Point(10, posicioY));
    	
    	llista = new BList();
    	llista.addValue(new String("800x600"));
    	llista.addValue(new String("1024x768"));
    	llista.setPreferredSize(150,GestorConstants.ALT_BOTO);
    	if(ElMensajeroDePekin.getResolucio()[0] == 800)
    		llista.setSelectedValue(new String("800x600"));
    	else
    		llista.setSelectedValue(new String("1024x768"));
    	
    	window.add(llista,new Point(200,posicioY));
    }
    
    private void crearCheckBoxAudio(int posicioY)
    {
    	text = new BLabel("So (engegat/apagat)");
    	text.setPreferredSize(180, GestorConstants.ALT_BOTO);
    	window.add(text, new Point(10, posicioY));
    	
    	checkBox = new BCheckBox("");
    	checkBox.setPreferredSize(GestorConstants.ALT_BOTO, GestorConstants.ALT_BOTO);
    	checkBox.setSelected(ElMensajeroDePekin.isSo());
    	
    	window.add(checkBox, new Point(253,posicioY));
    }
    
    private void crearCheckBoxPantallaCompleta(int posicioY)
    {
    	text = new BLabel("Pantalla Completa");
    	text.setPreferredSize(180, GestorConstants.ALT_BOTO);
    	window.add(text, new Point(10, posicioY));
    	
    	checkBox = new BCheckBox("");
    	checkBox.setPreferredSize(GestorConstants.ALT_BOTO, GestorConstants.ALT_BOTO);
    	checkBox.setSelected(ElMensajeroDePekin.isPantallaCompleta());
    	
    	window.add(checkBox, new Point(253,posicioY));
    }
    
    @Override
    public void render(float tpf) {
        DisplaySystem.getDisplaySystem().getRenderer().clearBuffers();
        rootNode.draw(DisplaySystem.getDisplaySystem().getRenderer());
    }
 
    @Override
    public void cleanup() {
        //Do nothing
    }
 
    @Override
    public void update(float tpf) {
    	super.update(tpf);
		//input.update(tpf);
    }
}
